let indexModule=(function(){
    let $plan = $.Callbacks();
    let $baseBox=$('.baseBox'),
        $baseBoxspan=$baseBox.find('span'),
        $loginout=$baseBox.find('a'),
        $menuBox = $('.menuBox'),
		$navBoxList = $('.navBox>a'),
		$itemBoxList = null,
        $iframeBox = $('.iframeBox');
        
    //退出登录
    $plan.add((_,baseinfo)=>{
        
        $baseBoxspan.html(`您好,${baseinfo.name|| '' }`)

        $loginout.click(async()=>{
            let res=await axios.get('user/signout')
            if(res.code==0){
                window.location.href='login.html'
                return 
            }
            alert('网络问题退出失败！请重试！')
        })

    })

    $plan.add((power)=>{
        
        let str=''
        if(power.includes('userhandle')){
            //员工管理
            str+=`
            <div class="itemBox" text='员工管理'>
                <h3>
                    <i class="iconfont icon-yuangong"></i>
                    员工管理
                </h3>
                <nav class="item">
                    <a href="page/userlist.html" target="iframeBox">员工列表</a>
                    <a href="page/useradd.html" target="iframeBox">新增员工</a>
                </nav>
            </div>
            `

        }
        if(power.includes('departhandle')){
            //部门管理
            str+=`
            <div class="itemBox" text='部门管理'>
                <h3>
                    <i class="iconfont icon-guanliyuan"></i>
                    部门管理
                </h3>
                <nav class="item">
                    <a href="page/departmentlist.html" target="iframeBox">部门列表</a>
                    <a href="page/departmentadd.html" target="iframeBox">新增部门</a>
                </nav>
            </div>
            `
            
        }
        if(power.includes('jobhandle')){
            //职务管理
            str+=`
            <div class="itemBox" text='职务管理'>
                <h3>
                    <i class="iconfont icon-zhiwuguanli"></i>
                    职务管理
                </h3>
                <nav class="item">
                    <a href="page/joblist.html" target="iframeBox">职务列表</a>
                    <a href="page/jobadd.html" target="iframeBox">新增职务</a>
                </nav>
            </div>
            `
        }
        
        if(power.includes('customer')){
            //客户管理
            str+=`
            <div class="itemBox" text='客户管理'>
                <h3>
                    <i class="iconfont icon-kehuguanli"></i>
                    客户管理
                </h3>
                <nav class="item">
                <a href="page/customerlist.html?lx=my" target="iframeBox">我的客户</a>
                ${power.includes('customerall')?`<a href="page/customerlist.html?lx=all" target="iframeBox">全部客户</a>`:``}
                <a href="page/customeradd.html" target="iframeBox">新增客户</a>
                </nav>
            </div>
            `
        }
        $menuBox.html(str)
        $itemBoxList = $menuBox.find('.itemBox');

    })

    function handlegroup(index){
        console.log(index,'index')
        let group1=$itemBoxList.filter((_,item)=>{
            
            let text=$(item).attr('text')
            
            return /^(员工管理|部门管理|职务管理)$/.test(text)
        })
        let group2=$itemBoxList.filter((_,item)=>{
            
            let text=$(item).attr('text')
            
            return text==='客户管理'
        })

        if(index==0){
            group1.css('display', 'block');
			group2.css('display', 'none');
        }else if(index==1){
           
            group1.css('display', 'none');
            group2.css('display', 'block');
        }
    }

    $plan.add(power=>{
        let defaultindex=power.includes('customer') ? 0: 1
        console.log(defaultindex)
        $navBoxList.eq(defaultindex).addClass('active').siblings().removeClass('active');
        handlegroup(defaultindex)

        $navBoxList.click(function (){
            let $this = $(this)
				index = $this.index(),
                text = $this.html().trim();
                console.log($this,index,text)
                if ((text === "客户管理" && !/customer/.test(power)) || (text === "组织结构" && !/(userhandle|departhandle|jobhandle)/.test(power))) {
                    alert('小主，您无权限访问改操作，请先联系系统管理员~~');
                    return;
                }
                if (index === defaultindex) return;
                $this.addClass('active');
                $navBoxList.eq(defaultindex).removeClass('active');
                handlegroup(index);
                defaultindex = index;
                
        })
    })

    $plan.add(power=>{
        let url = 'page/customerlist.html?lx=my';
		if (!power.includes('customer')) {
			url = 'page/userlist.html';
		}
		$iframeBox.attr('src', url);
    })

    return{
        async init(){
            let result=await axios.get('/user/login')
            if(result.code!==0){
                alert('您未登录，请先登录！')
                window.location.href='login.html'
                return 
            }
            let [userpower,baseinfo]=await axios.all([
                axios.get('/user/power'),
                axios.get('user/info')
            ])

            userpower.code==0? userpower=userpower.power:null
            baseinfo.code==0? baseinfo=baseinfo.data:null
            //发布订阅模式
            $plan.fire(userpower,baseinfo)
        }

    }
})()
indexModule.init()